package cn.wolfcode.wolfcodemall.service.impl;

import cn.wolfcode.wolfcodemall.domain.Product;
import cn.wolfcode.wolfcodemall.domain.ProductImage;
import cn.wolfcode.wolfcodemall.domain.Property;
import cn.wolfcode.wolfcodemall.domain.PropertyValue;
import cn.wolfcode.wolfcodemall.mapper.ProductImageMapper;
import cn.wolfcode.wolfcodemall.mapper.ProductMapper;
import cn.wolfcode.wolfcodemall.mapper.PropertyMapper;
import cn.wolfcode.wolfcodemall.mapper.PropertyValueMapper;
import cn.wolfcode.wolfcodemall.qo.PageQo;
import cn.wolfcode.wolfcodemall.qo.ProductQo;
import cn.wolfcode.wolfcodemall.service.ProductService;
import cn.wolfcode.wolfcodemall.vo.ProductVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductImageMapper productImageMapper;
    @Autowired
    private PropertyMapper propertyMapper;
    @Autowired
    private PropertyValueMapper propertyValueMapper;
    public PageInfo<Product> selectByPage(PageQo qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());

        List<Product> productList = productMapper.selectAll();

        return new PageInfo<Product>(productList);
    }

    public PageInfo<Product> selectByCondition(ProductQo qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());

        List<Product> productList = productMapper.selectByCondition(qo);

        return new PageInfo<Product>(productList);
    }
    @Transactional
    public void saveOrUpdate(ProductVo vo) {
        //区分当前是添加还是修改操作
        if(vo.getProductId() == null){
            //添加
            //保存商品信息
            //设置添加时间
            vo.setProductCreateDate(new Date());
            productMapper.insert(vo);
        }else{
            //修改商品信息
            productMapper.updateByPrimaryKey(vo);
            //删除商品原有的图片信息
            productImageMapper.deleteByProductId(vo.getProductId());
            //删除商品原有的属性值信息
            propertyValueMapper.deleteByProductId(vo.getProductId());
        }
        //保存商品的概述图片信息    productimage
        String[] singlePicture = vo.getSinglePicture();
        if(singlePicture != null && singlePicture.length > 0){
            for (String picUrl : singlePicture) {
                productMapper.insertProductImage(0,picUrl,vo.getProductId());
            }
        }
        //保存商品的详情图片信息    productimage
        String[] detailsPicture = vo.getDetailsPicture();
        if(detailsPicture != null && detailsPicture.length > 0){
            for (String picUrl : detailsPicture) {
                productMapper.insertProductImage(1,picUrl,vo.getProductId());
            }
        }
        //保存商品的属性值信息    propertyvalue
        Map<Integer, String> propertys = vo.getPropertys();
        if(propertys != null && propertys.size() > 0){
            Set<Map.Entry<Integer, String>> entries = propertys.entrySet();
            for (Map.Entry<Integer, String> entry : entries) {
                productMapper.insertProductPropertyValue(entry.getValue(),entry.getKey(),vo.getProductId());
            }
        }
    }
    public Product selectById(Integer productId) {
        //查询商品信息
        Product product = productMapper.selectByPrimaryKey(productId);

        //查询商品的概述图片
        List<ProductImage> productSinglePicList = productImageMapper.selectProductSinglePic(productId);
        product.setProductSinglePicList(productSinglePicList);

        //查询商品详情图片
        List<ProductImage> productDetailsPicList = productImageMapper.selectProductDetailsPic(productId);
        product.setProductDetailsPicList(productDetailsPicList);

        //查询商品的属性值信息
        //1.根据分类的id查询商品的属性信息
        List<Property> propertyList = propertyMapper.selectByCategoryId(product.getProductCategoryId());
        //2.在根据属性的id查询属性值信息
        if(propertyList != null && propertyList.size() > 0){
            for (Property property : propertyList) {
                //根据属性id以及商品id查询属性值
                List<PropertyValue> propertyValues = propertyValueMapper.selectByPropertyIdAndProductId(property.getPropertyId(), product.getProductId());
                //将属性值存储到属性对象中
                property.setPropertyValueList(propertyValues);
            }
        }
        //3.将属性信息存储到商品对象中
        product.setPropertyList(propertyList);

        return product;
    }

    public List<Product> selectHomeProductByCategoryId(Integer categoryId) {
        List<Product> products = productMapper.selectHomeProductByCategoryId(categoryId);
        if(products != null && products.size() > 0){
            for (Product product : products) {
                //查询商品的概述图片
                List<ProductImage> productSinglePic = productImageMapper.selectProductSinglePic(product.getProductId());
                product.setProductSinglePicList(productSinglePic);
            }
        }
        return products;
    }

    @Override
    public List<Product> selectByUser(String productName) {
        List<Product> products = productMapper.selectByUser(productName);
        return products;
    }

    @Override
    public List<Product> selectBySort(String orderField, String orderType) {
        return productMapper.selectBySort(orderField, orderType);
    }

    @Override
    public List<Product> selectByCategoryId(Integer categoryId) {
        return productMapper.selectByCategoryId(categoryId);
    }
}
